package com.example.demo.controller;

import com.example.demo.core.ICommander;
import com.example.demo.pojo.Slave;
import com.example.demo.service.SlaveService;
import com.example.demo.util.DefaultException;
import com.example.demo.util.JsonBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
public class AppController {

    @Autowired
    SlaveService service;

    @RequestMapping("/slave/list")
    public JsonBody getAllSlave() {
        return JsonBody.ok().put("data",service.getAllSlave());
    }

    @RequestMapping("/slave/{uuid}/volume/{level}")
    public JsonBody setSlaveVolume(@PathVariable String uuid, @PathVariable String level) throws DefaultException {
        Slave slave = new Slave();
        slave.setMaster_volume(Integer.parseInt(level));
        slave.setUuid(uuid);
        if (service.setMasterVolume(slave)) {
            return JsonBody.ok();
        }
        return JsonBody.error();
    }
}
